package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import DBPack.BusStationTime;
import DBPack.FerryTime;
import Drivers.BusDriver;
import Drivers.FerryDriver;

/**
 * Servlet implementation class getFerryTimeList
 */
public class getFerryTimeList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getFerryTimeList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int ferryId=Integer.parseInt(request.getParameter("ferry_id"));
		String direction=request.getParameter("direction");
		try {
			
			FerryTime[] list=(direction==null?FerryDriver.getFerryTimeList(ferryId):FerryDriver.getFerryTimeList(ferryId, direction));
			if (list==null)
				return;
			response.setContentType("text/html; charset=UTF-8");
		    response.setCharacterEncoding("UTF-8");

			PrintWriter out = response.getWriter();
			JSONArray x=new JSONArray();
			JSONObject y; 	
			for (int i=0;i<list.length;i++) {
				y=new JSONObject();
						
					
					y.put("timeId", list[i].getTimeId());
					y.put("ferryId", list[i].getFerryId());
					y.put("name", list[i].getName());
					y.put("time", list[i].getTime());
					y.put("direction", list[i].getDirection());
					y.put("dayCode", list[i].getDayCode());
					x.put(y);
			}	
			out.print(x);
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
